package com.whfc.ms.api.safety.service.impl;

import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.result.ListData;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.RandomUtil;
import com.whfc.fuum.dto.SysUserDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.service.SysUserService;
import com.whfc.ms.api.safety.service.MsSafetyCheckService;
import com.whfc.quality.dto.QualityCheckStatisticalDTO;
import com.whfc.safety.dto.*;
import com.whfc.safety.param.SafetyCheckListParam;
import com.whfc.safety.param.SafetyCheckLogParam;
import com.whfc.safety.param.SafetyCheckParam;
import com.whfc.safety.param.SafetyCheckUserParam;
import com.whfc.safety.service.SafetyCheckService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClasssName MsSafetyCheckServiceImpl
 * @Description 质量管理问题上报实现类
 * @Author hw
 * @Date 2020/8/3 11:01
 * @Version 1.0
 */
@Service
public class MsSafetyCheckServiceImpl implements MsSafetyCheckService {

    @DubboReference(interfaceClass = SafetyCheckService.class, version = "1.0.0")
    private SafetyCheckService safetyCheckService;

    @DubboReference(interfaceClass = SysUserService.class, version = "1.0.0")
    private SysUserService sysUserService;

    /**
     * 安全图片路径
     */
    private String safetyImgOssDir = "safety/img";

    @Autowired
    private FileHandler fileHandler;

    @Autowired
    private FilePathConfig filePathConfig;

    @Override
    public SafetyCheckListDTO getCheckList(Integer deptId, Integer[] type, Integer[] state, Date startTime,
                                           Date endTime, Integer partId, Integer urgency, Integer overdue, Integer userId, Integer pageNum, Integer pageSize) {
        SafetyCheckListParam checkListParam = new SafetyCheckListParam();
        checkListParam.setUserId(userId);
        checkListParam.setType(type == null || type.length <= 0 ? null : type);
        checkListParam.setDeptId(deptId);
        checkListParam.setEndTime(endTime);
        checkListParam.setOverdue(overdue);
        checkListParam.setPartId(partId);
        checkListParam.setStartTime(startTime);
        checkListParam.setState(state == null || state.length <= 0 ? null : state);
        checkListParam.setUrgency(urgency);
        if (pageNum != null && pageSize != null) {
            checkListParam.setPageSize(pageSize);
            checkListParam.setPageNum(pageNum);

        }
        return safetyCheckService.list(checkListParam);
    }

    @Override
    public SafetyCheckDescDTO desc(Integer deptId) {
        return safetyCheckService.desc(deptId);
    }

    @Override
    public void add(SafetyCheckParam checkParam, SysUser user) {
        SafetyCheckUserParam userParam = new SafetyCheckUserParam();
        userParam.setType(1);
        userParam.setUserId(user.getId());
        userParam.setUserName(user.getNickname());
        userParam.setUserPhone(user.getPhone());
        checkParam.getCheckUsers().add(userParam);
        List<SafetyCheckUserParam> safetyCheckUserParams = checkParam.getCheckUsers().stream().filter(Objects::nonNull).collect(Collectors.toList());
        Collections.sort(safetyCheckUserParams);
        checkParam.setCheckUsers(safetyCheckUserParams);

        checkParam.setCheckUserId(user.getId());
        checkParam.setCheckUserName(user.getNickname());
        safetyCheckService.addCheck(checkParam);

    }

    @Override
    public void del(Integer checkId) {
        safetyCheckService.delCheck(checkId);
    }

    @Override
    public List<SysUserDTO> sysUserList(Integer deptId) {
        return sysUserService.getUsersByDeptId(deptId);
    }

    @Override
    public void addLog(SafetyCheckLogParam safetyCheckLogParam) {
        //发送消息
        safetyCheckService.addCheckLog(safetyCheckLogParam);
    }

    @Override
    public SafetyCheckStatisticalDTO statistics(Integer deptId, Date startTime, Date endTime) {
        return safetyCheckService.statistical(deptId, startTime, endTime, 2);
    }
    @Override
    public SafetyCheckStatisticalDTO statistics(Integer deptId, Integer userId, Date startTime, Date endTime) {
        return safetyCheckService.statistical(deptId, userId, startTime, endTime, 2);
    }

    @Override
    public SafetyCheckStatisticalDTO statisticsDay(Integer deptId, Integer userId, Date dayTime) {
        Date startTime = DateUtil.getDateBegin(dayTime);
        Date endTime = DateUtil.getDateEnd(dayTime);
        return safetyCheckService.statistical(deptId, userId, startTime, endTime, 2);
    }

    @Override
    public SafetyCheckStatisticalDTO statisticsMonth(Integer deptId, Integer userId, Date monthTime) {
        Date startTime = DateUtil.getMonthBegin(monthTime);
        Date endTime = DateUtil.getMonthEnd(monthTime);
        return safetyCheckService.statistical(deptId, userId, startTime, endTime, 2);
    }

    @Override
    public String uploadImg(MultipartFile file) {
        try {
            String fileName = RandomUtil.getRandomFileName() + ".png";
            InputStream inputStream = file.getInputStream();
            String ossKey = filePathConfig.getFilePath(safetyImgOssDir, fileName);
            return fileHandler.upload(ossKey, inputStream);
        } catch (IOException e) {
            return null;
        }
    }

    @Override
    public SafetyLogDTO safetyLogLog(Integer deptId, Date date) {
        return safetyCheckService.safetyLog(deptId, date);
    }

    @Override
    public void export(String safetyIds, Integer deptId) {
        String[] strIds = safetyIds.split(",");
        List<Integer> intIds = Arrays.stream(strIds).map(Integer::valueOf).collect(Collectors.toList());
        safetyCheckService.safetyExport(intIds, deptId);
    }

    @Override
    public SafetyCheckScreenDTO safetyCheckScreenStatistical(Integer deptId, Date startTime, Date endTime) {
        return safetyCheckService.safetyCheckScreenStatistical(deptId, startTime, endTime);
    }

    @Override
    public SafetyCheckScreenDTO safetyCheckTypeAnalysis(Integer deptId, Date startTime, Date endTime) {
        return safetyCheckService.safetyCheckTypeAnalysis(deptId, startTime, endTime);
    }

    @Override
    public SafetyCheckUnfinishedDTO safetyCheckUnfinished(Integer deptId, Date startTime, Date endTime) {
        return safetyCheckService.safetyCheckUnfinished(deptId, startTime, endTime);
    }

    @Override
    public ListData<SafetyCheckPartStatisticalDTO> safetyCheckPartAnalysis(Integer deptId, Date startTime, Date endTime) {
        List<SafetyCheckPartStatisticalDTO> list = safetyCheckService.safetyCheckPartAnalysis(deptId, startTime, endTime);
        return new ListData<>(list);
    }

    @Override
    public void deleteBatch(List<Integer> idList) {
        safetyCheckService.deleteBatch(idList);
    }
}
